package com.jic.market.enums;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yinhaijun
 * @date: 2020/3/13
 */
public enum FrontPromotionTypeEnum {
    SINGLELY_SPECIAL(PromotionTypeEnum.SINGLELY, 1,  "特价", 4,"SingleSpecialProcessor"),
    SINGLELY_DISCOUNT(PromotionTypeEnum.SINGLELY, 2,  "折扣", 4,"SingleDiscountProcessor"),
    SINGLELY_FALL(PromotionTypeEnum.SINGLELY, 3,  "直降", 4,"SingleFallProcessor"),
    FULL_NUM_REDUCE(PromotionTypeEnum.FULL_NUM, 4,  "满量减", 3,"FullNumReduceProcessor"),
    FULL_NUM_DISCOUNT(PromotionTypeEnum.FULL_NUM, 5,  "满量折", 3,"FullNumDiscountProcessor"),
    FULL_REDUCE(PromotionTypeEnum.FULL, 6,  "满额减",3, "FullReduceProcessor"),
    FULL_DISCOUNT(PromotionTypeEnum.FULL, 7,  "满额折",3, "FullDiscountProcessor"),
    GIFT_FULL_QUOTA(PromotionTypeEnum.GIFT, 8,  "满额赠", 2,"GiftFullQuotaProcessor"),
    GIFT_FULL_NUM(PromotionTypeEnum.GIFT, 9,  "满量赠", 2,"GiftFullNumProcessor"),
    SECKILL_SECKILL(PromotionTypeEnum.SECKILL, 10,  "秒杀", 4,"SeckillProcessor"),
//    SECKILL_SECKILL1(PromotionTypeEnum.SECKILL, 11,  "买赠"),//
    XOFFER_DI_X_Y_ZE(PromotionTypeEnum.XOFFER, 12,  "第X件Y折", 1,"XofferDiXyZeProcessor"),
    XOFFER_DI_X_Y_YUAN(PromotionTypeEnum.XOFFER, 13,  "第X件Y元", 1,"XofferDiXyYuanProcessor"),
    XOFFER_MAN_X_Y_ZE(PromotionTypeEnum.XOFFER, 14,  "满X件Y折", 3,"XofferManXyZeProcessor"),//多品中的单品
    INCREASE_FULL_QUOTA(PromotionTypeEnum.increase, 15,  "满额换购",2,"ChangeFullQuotaProcessor"),
    INCREASE_FULL_NUM(PromotionTypeEnum.increase, 16,  "满量换购", 2,"ChangeFullNumProcessor"),
    BALANCE_CALCULATION(PromotionTypeEnum.SINGLE, 17,  "尾款计算", 4,"BalanceCalculationProcessor"),
    BITE_PRICE(PromotionTypeEnum.SINGLE, 18, "一口价", 4,"BitePriceProcessor"),

    FULL_REDUCE_LOGISTICS(PromotionTypeEnum.FULL, 19,  "满额减运费", 3,"FullReduceLogisticsProcessor"),
    INCREASE_BUY_GOODS(PromotionTypeEnum.increase, 20,  "买商品换购", 3,""),
//    INCREASE_FULL_NUM(PromotionTypeEnum.increase, 21,  "满金额换购"),//
    COMPLEX_X_YUAN_Y(PromotionTypeEnum.COMPLEX, 22,  "X元Y件", 3,"ComplexXyuanyProcessor"),
    COMPLEX_X_JIAN_FU_Y(PromotionTypeEnum.COMPLEX, 23,  "X件付Y件",3 ,"ComplexXjianFuyProcessor"),//多品中的单品
    COUPON_(PromotionTypeEnum.Coupon, 24,  "优惠劵", 6,"CouponDiscountProcessor"),
    RED(PromotionTypeEnum.RED, 25,  "红包", 6,""), //红包暂时待定
    GROUPON(PromotionTypeEnum.GROUPON, 26,  "拼团", 5,"GrouponDiscountProcessor"),
    FULL_NUM_REDUCE_LOGISTICS(PromotionTypeEnum.FULL_NUM, 27,  "满量减运费", 3,"FullNumReduceLogisticsProcessor"),//运费待定
    LOTTERY(PromotionTypeEnum.LOTTERY, 28,  "抽奖", 6,""),//抽奖待修改
    PAYMENT_REDUCTION(PromotionTypeEnum.PAYMENT_REDUCTION, 29,  "支付立减", 7,"PayProcessor"),//we2000支付立减
    WECHANT_PAY(PromotionTypeEnum.PAYMENT_REDUCTION, 30,  "微信支付支付立减", 7,"PayProcessor"),//微信支付支付立减
    WEBANK_PAY(PromotionTypeEnum.PAYMENT_REDUCTION, 31,  "微众卡支付立减", 7,"PayProcessor"),//微众卡支付立减
    COMPANY(PromotionTypeEnum.PAYMENT_REDUCTION, 32,  "企业购支付立减", 7,"PayProcessor"),//企业购支付立减
    ;

    private PromotionTypeEnum promType;
    private Integer code;
    private String message;
    private Integer group;//分组,见图
    private String processor;

    FrontPromotionTypeEnum(PromotionTypeEnum promType, Integer code, String message, Integer group, String processor) {
        this.promType = promType;
        this.code = code;
        this.message = message;
        this.group = group;
        this.processor = processor;
    }

    public static FrontPromotionTypeEnum getByCode(Integer code){
        FrontPromotionTypeEnum[] values = FrontPromotionTypeEnum.values();
        for(FrontPromotionTypeEnum value : values){
            if(value.getCode().equals(code)){
                return value;
            }
        }
        return null;
    }

    public Boolean isSinglePromotion(){
        if(this.getGroup().equals(1)
            || this.getGroup().equals(4)
            || this.getGroup().equals(5)){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    public Boolean isConditionPromotion(){
        if(this.getGroup().equals(2)
                || this.getGroup().equals(3)){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /** 单品类活动互斥的 */
    public static List<Integer> getExclusiveSingleCodes(Integer code, FrontPromotionTypeEnum source) {
        List<Integer> exclusiveCodes = getExclusiveCodes(code);
        if(null == exclusiveCodes || exclusiveCodes.isEmpty()){
            return null;
        }
        for(int i = exclusiveCodes.size() - 1; i>=0; i--){
            List<Integer> singleGourp = new ArrayList<>();
            singleGourp.add(1);
            singleGourp.add(4);
            singleGourp.add(5);
            if(!singleGourp.contains(FrontPromotionTypeEnum.getByCode(exclusiveCodes.get(i)).getGroup())){
                exclusiveCodes.remove(i);
            }
        }
        if(null != source){
            if(!exclusiveCodes.contains(source.getCode())){
                return null;
            }
            List<Integer> sourceList = new ArrayList<>();
            sourceList.add(source.getCode());
            return sourceList;
        }
        return exclusiveCodes;
    }
    /** 多品类活动互斥的 */
    public static List<Integer> getExclusiveConditionCodes(Integer code) {
        List<Integer> exclusiveCodes = getExclusiveCodes(code);
        if(null == exclusiveCodes || exclusiveCodes.isEmpty()){
            return null;
        }
        for(int i = exclusiveCodes.size() - 1; i>=0; i--){
            List<Integer> singleGourp = new ArrayList<>();
            singleGourp.add(2);
            singleGourp.add(3);
            if(!singleGourp.contains(FrontPromotionTypeEnum.getByCode(exclusiveCodes.get(i)).getGroup())){
                exclusiveCodes.remove(i);
            }
        }
        return exclusiveCodes;
    }

    /** 查询互斥的 */
    public static List<Integer> getExclusiveCodes(Integer code){
        Integer sourceGroup = getByCode(code).getGroup();
        // 第一组，互斥组为1、3、4、5组，共享组为2组
        if(sourceGroup.equals(1)){
            List<Integer> groupList = new ArrayList();
            groupList.add(3);
            groupList.add(4);
            groupList.add(5);
            List<Integer> codeList = getCodesByGroupList(groupList);
            return codeList;
        }
        // 第二组，互斥组为2、5组，组内仅和自身互斥，满量和买M赠送N互斥
        else if(sourceGroup.equals(2)){
            List<Integer> groupList = new ArrayList();
            groupList.add(5);
            List<Integer> groupList1 = getCodesByGroupList(groupList);
            groupList1.add(code);
            //TODO 满量和买M赠送N互斥
            if(code.equals(4) || code.equals(5)){
//                groupList1.add()
            }
            return groupList1;
        }
        // 第三组，互斥组1、3、5组
        else if(sourceGroup.equals(3)) {
            List<Integer> groupList = new ArrayList();
            groupList.add(1);
            groupList.add(3);
            groupList.add(5);
            List<Integer> codes = getCodesByGroupList(groupList);
            if(code.equals(22) || code.equals(23)){
                codes.add(1);
                codes.add(2);
                codes.add(3);
                codes.add(10);
                codes.add(17);
                codes.add(18);
            }
            return codes;
        }
        // 第四组，互斥组1、4、5组
        else if(sourceGroup.equals(4)) {
            List<Integer> groupList = new ArrayList();
            groupList.add(1);
            groupList.add(4);
            groupList.add(5);
            List<Integer> groupList1 = getCodesByGroupList(groupList);
            groupList1.add(22);
            groupList1.add(23);
            return groupList1;
        }
        // 第五组，互斥组1、2、3、4组
        else if(sourceGroup.equals(5)) {
            List<Integer> groupList = new ArrayList();
            groupList.add(1);
            groupList.add(2);
            groupList.add(3);
            groupList.add(4);
            List<Integer> codes = getCodesByGroupList(groupList);
            if(code .equals(26)){
                codes.add(26);
            }
            return codes;
        }
        else if(sourceGroup.equals(6)){
            if(code.equals(25)){
                List<Integer> groupList25 = new ArrayList<>();
                groupList25.add(code);
                return groupList25;
            }
        }
        return null;
    }
    public static void main(String[] args){
        FrontPromotionTypeEnum[] values = FrontPromotionTypeEnum.values();
        for(FrontPromotionTypeEnum value : values) {
            List<Integer> excCodes = getExclusiveCodes(value.getCode());
            List<String> exeCodeMesList = new ArrayList<>();
            if(null != excCodes && !excCodes.isEmpty()){
                for(Integer c : excCodes){
                    exeCodeMesList.add(FrontPromotionTypeEnum.getByCode(c).getMessage());
                }
            }
            System.out.println("活动：" + value.getMessage() + "的互斥有：" + JSON.toJSONString(exeCodeMesList));
        }
//        for(FrontPromotionTypeEnum value : values) {
//            System.out.println("活动：" + value.getMessage() + "的单品互斥有：" + JSON.toJSONString(getExclusiveSingleCodes(value.getCode(), null)));
//        }
//        for(FrontPromotionTypeEnum value : values) {
//            System.out.println("活动：" + value.getMessage() + "的多品互斥有：" + JSON.toJSONString(getExclusiveConditionCodes(value.getCode())));
//        }
    }

    public static List<Integer> getCodesByGroupList(List<Integer> groupList){
        FrontPromotionTypeEnum[] values = FrontPromotionTypeEnum.values();
        List<Integer> codes = new ArrayList<>();
        for(FrontPromotionTypeEnum value : values){
            if(groupList.contains(value.getGroup())){
                codes.add(value.getCode());
            }
        }
        return codes;
    }

    /**
     * 根据组别返回对应的活动的code集合
     * @param group 组别
     * @param source 如果该值不为空，直接返回该值
     * @return
     */
    public static List<Integer> getCodesByGroup(Integer group, FrontPromotionTypeEnum source){
        FrontPromotionTypeEnum[] values = FrontPromotionTypeEnum.values();
        List<Integer> codes = new ArrayList<>();
        for(FrontPromotionTypeEnum value : values){
            if(value.getGroup().equals(group) && !value.getProcessor().equals("")){
                codes.add(value.getCode());
            }
        }
        if(null != source){
            if(!codes.contains(source.getCode())){
                return null;
            }
            List<Integer> sourceCodes = new ArrayList<>();
            sourceCodes.add(source.getCode());
            return sourceCodes;
        }
        return codes;
    }


    public PromotionTypeEnum getPromType() {
        return promType;
    }

    public void setPromType(PromotionTypeEnum promType) {
        this.promType = promType;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getProcessor() {
        return processor;
    }

    public void setProcessor(String processor) {
        this.processor = processor;
    }

    public Integer getGroup() {
        return group;
    }

    public void setGroup(Integer group) {
        this.group = group;
    }
}
